package com.ruge.test.springMVC.监听器;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.ArrayList;

/**
 * 创建人 ：爱丽丝、如歌
 * 创建时间 ：2018-07-01  9:54
 * 描述 ：统计在线人数
 */
@WebListener
public class OnLineCount implements HttpSessionListener {

    private int OnLineCount = 0;

    public void sessionCreated(HttpSessionEvent se) {
        OnLineCount++;
        se.getSession().getServletContext().setAttribute("OnLineCount", OnLineCount);
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        OnLineCount--;

        se.getSession().getServletContext().setAttribute("OnLineCount", OnLineCount);

        ArrayList<UserDetail> UserDetails = null;

        UserDetails = (ArrayList<UserDetail>) se.getSession().getServletContext().getAttribute("UserDetails");

        if (SessionUtil.getUserBySessionId(UserDetails, se.getSession().getId()) != null) {
            UserDetails.remove(SessionUtil.getUserBySessionId(UserDetails, se.getSession().getId()));
        }
    }
}
